package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.db.DB;

import com.vo.Users;

public class UserDaoImp implements IUserDao {

	Connection con;
	DB db = new DB();
	//登陆
	public boolean login(Users users) throws Exception {
		//得到数据库连接
		
		//连接对象的地址放在了con
		 con = db.getConn();
		//得到一个操作数据库的类
		String sql ="select eu_user_id,eu_password,eu_user_name,eu_status from easybuy_user where eu_user_name=? and eu_password=?";
		PreparedStatement ps = con.prepareStatement(sql);
		//给?注入值
		ps.setString(1, users.getEu_user_name());
		ps.setString(2, users.getEu_password());
		
		//操作数据库   
		//查询方法executeQuery()
		ResultSet rs = ps.executeQuery();
		boolean flag = rs.next();
		ps.close();
		DB dbs = new DB();
		dbs.closeConn();
		
		return flag;
	}
	//显示所有用户
	public List<Users> getAllData() throws Exception {
		// TODO Auto-generated method stub
		con = db.getConn();
		String sql = "select * from easybuy_user";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<Users> list = new ArrayList();
		while(rs.next()){
			Users u = new Users();
			u.setEu_user_id(rs.getInt(1));
			u.setEu_user_name(rs.getString(2));
			u.setEu_password(rs.getString(3));
			u.setEu_sex(rs.getString(4));
			u.setEu_birthday(rs.getString(5));
			u.setEu_email(rs.getString(7));
			u.setEu_mobile(rs.getString(8));
			u.setEu_address(rs.getString(9));
			u.setEu_status(rs.getInt(10));
			list.add(u);
		}
		rs.close();
		ps.close();
		db.closeConn();
		return list;
	}
	//注册
	public int register(Users users) throws Exception {
		// TODO Auto-generated method stub
		
		DB db = new DB();
		Connection con = db.getConn();
		
		
		String sql = "INSERT INTO easybuy_user (eu_user_name,eu_password)VALUES(?,?)";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1,users.getEu_user_name());
		ps.setString(2, users.getEu_password());
		//返回值 >0 更新成功
		int a = ps.executeUpdate();
		
		ps.close();
		db.closeConn();
		return a;
		
	}
	
	//添加用户
	public int userAdd(Users users) throws Exception {
		// TODO Auto-generated method stub
		DB db = new DB();
		
		int a = 0;
		Connection conn = db.getConn();
		String sql = "INSERT INTO easybuy_user(eu_user_name,eu_password,eu_sex,eu_birthday,eu_mobile,eu_address,eu_status)VALUES(?,?,?,?,?,?,'2');";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, users.getEu_user_name());
		ps.setString(2, users.getEu_password());
		ps.setString(3, users.getEu_sex());
		ps.setString(4, users.getEu_birthday());
		ps.setString(5, users.getEu_mobile());
		ps.setString(6, users.getEu_address());
		a = ps.executeUpdate();
		ps.close();
		db.closeConn();
		return a;
		
		
	}
	//修改用户信息
	public int updateUser(Users users) throws Exception {
		// TODO Auto-generated method stub
		DB db = new DB();
		Connection con = db.getConn();
		
		String sql ="UPDATE easybuy_user SET eu_user_name =?,eu_password =?,eu_sex = ?,eu_birthday =?,eu_mobile =?,eu_address =? WHERE eu_user_id=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, users.getEu_user_name());
		ps.setString(2, users.getEu_password());
		ps.setString(3, users.getEu_sex());
		ps.setString(4, users.getEu_birthday());
		ps.setString(5, users.getEu_mobile());
		ps.setString(6, users.getEu_address());
		ps.setInt(7, users.getEu_user_id());
		int a = ps.executeUpdate();
		
		ps.close();
		db.closeConn();
		return a;
	}
	//删除用户信息
	public int deleteUser(int id) throws Exception {
		// TODO Auto-generated method stub
		DB db = new DB();
		Connection con = db.getConn();
		String sql = "DELETE FROM easybuy_user WHERE eu_user_id = "+id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		int a = ps.executeUpdate();
		ps.close();
		db.closeConn();
		
		return a;
	}
	
	
	//测试数据库是否能查询数据
		public static void main(String[] args){
			IUserDao userdao = new UserDaoImp();
			//List<Users> list = new ArrayList<Users>();
			Users users = new Users();
			int id = 9;			
			try {
				int a = userdao.deleteUser(id);
				System.out.println(a);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
//			users.setEu_user_name("Man");
//			users.setEu_password("man");
//			users.setEu_sex("男");
//			users.setEu_birthday("2000-3-18");
//			users.setEu_mobile("18374771718");
//			users.setEu_address("湖南衡阳祁东");
//			users.setEu_user_id(11);
//			try {
//				int a = userdao.updateUser(users);
//				System.out.println(a);
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
//			
//			try {
//				List<Users> list = userdao.getAllData();
//				for(int i = 0;i < list.size();i++){
//					Users user = list.get(i);
//					int a = user.getEu_user_id();
//					String b = user.getEu_user_name();
//					String c=  user.getEu_password();
//					String d = user.getEu_birthday();
//					System.out.println(a+b+c+d);
//				}
//				
//				//boolean a = userdao.login(users);
//				System.out.println(users.getEu_birthday());
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
			

			
			
		}

		

		

		
		

		
}
